package com.yn.front.controller;


import com.yn.common.request.driver.UpdatePwdRequest;
import com.yn.common.request.sc.LoginMobileRequest;
import com.yn.common.request.sc.LoginRequest;
import com.yn.common.response.sc.BaseResult;
import com.yn.common.response.sc.LoginResponse;
import com.yn.front.service.LoginService;
import com.yn.service.service.SmsService;
import com.yn.service.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户登陆 前端控制器
 *
 */
@Slf4j
@RestController("FrontLoginController")
@RequestMapping("api/front")
@Api(tags = "用户 -- 登录注册")
public class LoginController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private LoginService loginService;


    /**
     * 手机号登录接口
     */
    @ApiOperation(value = "手机号登录接口")
    @RequestMapping(value = "/login/mobile", method = RequestMethod.POST)
    public BaseResult<LoginResponse> phoneLogin(@RequestBody @Validated LoginMobileRequest loginRequest) {
        return BaseResult.success(loginService.phoneLogin(loginRequest));
    }

    /**
     * 账号密码登录
     */
    @ApiOperation(value = "账号密码登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResult<LoginResponse> login(@RequestBody @Validated LoginRequest loginRequest) {
        return BaseResult.success(loginService.login(loginRequest));
    }

    /**
     * 修改登陆手机号
     * */

    @ApiOperation(value = "手机号修改接口")
    @PutMapping("/driver/update/phone")
    public BaseResult<String> updatePhone(@RequestBody @Validated LoginMobileRequest loginRequest) {
        loginService.updatePhone(loginRequest);
        return BaseResult.success("修改成功");
    }

    /**
     * 设置新密码
     * */
    @ApiOperation(value = "修改密码接口")
    @PutMapping("/driver/update/pwd")
    public BaseResult<String> updatePwd(@RequestBody @Validated UpdatePwdRequest request) {
        loginService.updatePwd(request);
        return BaseResult.success("修改成功");
    }

    /**
     * 修改昵称
     * */
    @ApiOperation(value = "修改昵称接口")
    @PutMapping("/driver/update/nickName")
    public BaseResult<String> updateNickName(@RequestParam String nickName) {
        loginService.updateNickName(nickName);
        return BaseResult.success("修改成功");
    }
    /**
     * 修改头像
     * */
    @ApiOperation(value = "修改头像接口")
    @PutMapping("/driver/update/avatar")
    public BaseResult<String> updateAvatar(@RequestParam String avatar) {
        //查看数据库种是否存在该url
        loginService.updateAvatar(avatar);
        return BaseResult.success("修改成功");
    }


    /**
     * 退出登录
     */
    @ApiOperation(value = "退出")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public BaseResult<String> loginOut(HttpServletRequest request){
        loginService.loginOut(request);
        return BaseResult.success();
    }

    /**
     * 发送短信登录验证码
     * @param phone 手机号码
     * @return 发送是否成功
     */
    @ApiOperation(value = "发送短信登录验证码")
    @RequestMapping(value = "/sendCode", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone", value="手机号码", required = true)
    })
    public BaseResult<Object> sendCode(@RequestParam String phone){
        if(smsService.sendCommonCode(phone)){
            return BaseResult.success("发送成功");
        }else{
            return BaseResult.failed("发送失败");
        }
    }
}



